package com.wisdom.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import org.sipdroid.sipua.R;
import org.sipdroid.sipua.ui.Receiver;
import org.sipdroid.sipua.ui.Settings;

import android.content.Context;
import android.os.Environment;
import android.preference.PreferenceManager;

public final class 
CCPConfig {
	
	public static boolean check() {
		//if(!PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_USERNAME, "").equals("")){
			//return true;
		//}
		
		if (
//				Main_Account != null && !Main_Account.equals("")
				 REST_SERVER_ADDRESS != null && !REST_SERVER_PORT.equals("")
				//&& Main_Token != null && !Main_Token.equals("")
				&& Sub_Account != null && !Sub_Account.equals("")
				&& Sub_Token != null && !Sub_Token.equals("")
				&& VoIP_ID != null && !VoIP_ID.equals("")
				&& VoIP_PWD != null && !VoIP_PWD.equals("")
				&& App_ID != null && !App_ID.equals("")
				&& AccessToken != null && !AccessToken.equals("")
				&& UserId != null && !UserId.equals("")&&!UserId.equals("0")
				) {
			return true;
		}

		return false;
	}
	public static String Mobile_BasicUrl="http://www.1000call.com";
	/**
	 *  Demo rest server address info
	 */
	public static String REST_SERVER_ADDRESS = "app.cloopen.com";
	
	/**
	 *  Demo rest server port info
	 */
	public static String REST_SERVER_PORT = "8883";
	
	
	public static String Main_Account = "";
	public static String Main_Token = "";
	public static String Sub_Account = "";
	public static String Sub_Token = "";
	public static String Sub_Name = "";
	public static String VoIP_ID = "";
	public static String VoIP_PWD = "";
	public static String App_ID = "";
	public static String Src_phone = "";
	public static String AccessToken="";
	public static String UserId="";
	public static String Balance="";
	public static String CallSetting="";
	
	
	public static String LOCAL_PATH = getExternalStorePath() + "/config1000callV10.properties";
	private static Properties properties;
	
	public static String GetUserName(){
		return PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_USERNAME, "");
	}
	
	/**
	 * 初始化属性
	 */
	public static boolean initProperties(Context applicationContext) {
		if(properties==null){
			properties=new Properties();
		}
		if(isExistExternalStore()){
			 String content=readContentByFile(LOCAL_PATH);
			 if(content!=null){
				 try{
					 properties.load(new FileInputStream(LOCAL_PATH));
					 return loadConfigByProperties();
				 }catch(IOException e){
					 e.printStackTrace();
					 return false;
				 }
			 }
		}
		
		//if SDcar 
		try{
			properties.load(applicationContext.getResources().openRawResource(R.raw.config));
			return loadConfigByProperties();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
		
	}
	private static void saveConfig(Properties properties) { 
		try { 
		FileOutputStream s = new FileOutputStream(LOCAL_PATH, false); 
		properties.store(s, ""); 
		} catch (Exception e){ 
		e.printStackTrace(); 
		} 
		} 

	/**
	 * load config info from config.properties
	 */
	private static boolean loadConfigByProperties() {
		REST_SERVER_ADDRESS = properties.getProperty("server_address");
		REST_SERVER_PORT = properties.getProperty("server_port");
		
		
		Main_Account = properties.getProperty("main_account");
		Main_Token = properties.getProperty("main_token");
		Sub_Account/*Sub_Account*/ = properties.getProperty("sub_account");
		Sub_Token/*Sub_Token*/ = properties.getProperty("sub_token");
		VoIP_ID/*VoIP_ID*/ = properties.getProperty("voip_account");
		VoIP_PWD/*VoIP_PWD*/ = properties.getProperty("voip_password");
		App_ID = properties.getProperty("app_id");
		AccessToken=properties.getProperty("accesstoken");
		UserId=properties.getProperty("user_id");
		Src_phone=properties.getProperty("src_phone");
		Balance=properties.getProperty("balance");
		CallSetting=properties.getProperty("callsetting");
		return check();
	}
	 public static Properties getConfigProperties() {  
		 return properties;
	 }
	 public static void setConfigProperties(String key,String value){
		 properties.setProperty(key, value);
		 saveConfig(properties);
	 }
	public static String readContentByFile(String path) {
		BufferedReader reader = null;
		String line = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				StringBuilder sb = new StringBuilder();
				reader = new BufferedReader(new FileReader(file));
				while ((line = reader.readLine()) != null) {
					sb.append(line.trim());
				}
				return sb.toString().trim();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}
	/**
	 * /sdcard
	 *
	 * @return
	 */
	public static String getExternalStorePath() {
		if (isExistExternalStore()) {
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return null;
	}
	public static boolean isExistExternalStore() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
}
